﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using UnityEngine;

namespace CustomCommands
{
    public class ExtraMethods         //these are some basic methods that should cover most of the thing you need when writing commands
    {                                     //go to the myCommands class if you want to add your own commands  ->
        
        public static String[] AdminNames;
        public static String[] AdminSteamIDs;

        public static string[] playerNames = { "PLAYER NAMES HERE :D" };
        public static string[] playerSteamIDs = { "PLAYER STEAMIDS HERE :D" };

        private static NetworkChat networkchat;
        private static FieldInfo[] networkChatfields = typeof(NetworkChat).GetFields();
        private static FieldInfo[] networkUserfields = typeof(NetworkUser).GetFields();

        private static float updater;





        public static NetworkChat getNetworkChat()
        {
            return networkchat;
        }


        public static void readAdminsFromFile(String filename)
        {
            if (!File.Exists(filename))  //create a template for admins
            {
                System.IO.StreamWriter file = new StreamWriter(filename, true);
                file.WriteLine("Nessin:76561197976976379");
                file.WriteLine("Some Other Admin:12345789");

                file.Close();
            }
            string[] lines = System.IO.File.ReadAllLines(@filename);
            AdminNames = new String[lines.Length];
            AdminSteamIDs = new String[lines.Length];
            for (int i = 0; i < lines.Length; i++)
            {
                if (lines[i].Length > 10)
                {
                    AdminNames[i] = lines[i].Split(':')[0];
                    AdminSteamIDs[i] = lines[i].Split(':')[1];
                }

            }
        }

        public static void UpdatePlayerList(bool forceUpdate = false)
        {
            if (updater <= 1f || forceUpdate == true)
            {
                if (networkchat == null)
                    networkchat = UnityEngine.Object.FindObjectOfType(typeof(NetworkChat)) as NetworkChat;

                Player[] players = UnityEngine.Object.FindObjectsOfType(typeof(Player)) as Player[];
                playerNames = new String[players.Length];
                playerSteamIDs = new String[players.Length];

                for (int i = 0; i < players.Length; i++)
                {
                    playerNames[i] = players[i].name;

                    NetworkPlayer np = players[i].networkView.owner;
                    NetworkUser nu = NetworkUserList.getUserFromPlayer(np);

                    playerSteamIDs[i] = networkUserfields[3].GetValue(nu).ToString();

                }
                if (playerNames.Length == 0)
                {
                    playerNames = new String[1];
                    playerSteamIDs = new String[1];
                }
                updater = 500f;
            }
            updater--;
        }

        public static bool isAdmin(String name)
        {
            UpdatePlayerList(true);
            for (int i = 0; i < AdminNames.Length; i++)
            {
                if (AdminNames[i].Equals(name) && AdminSteamIDs[i].Equals(getSteamIDByPlayerName(name)))
                {

                    return true;
                }
            }
            return false;
        }

        public static String getLastMessagePlayerName()
        {
            return getMsgByNum(3);
        }

        public static String getLastMessageText()
        {
            return getMsgByNum(6);
        }


        private static String getMsgByNum(int num2)
        {
            try
            {
                return networkChatfields[num2].GetValue(networkchat).ToString();
            }
            catch
            {
                return "";
            }
        }


        public static String getSteamIDByPlayerName(String playerNaam)
        {
            for (int i = 0; i < playerNames.Length; i++)
            {
                if (playerNames[i].Equals(playerNaam))
                {
                    return playerSteamIDs[i];
                }
            }
            return "";
        }

        public static NetworkPlayer getNetworkPlayerByPlayerName(String playerNaam)
        {
            Player[] players = GameObject.FindObjectsOfType<Player>();
            NetworkPlayer np = players[0].networkView.owner;
            foreach (Player p in players)
            {
                if (p.name.Equals(playerNaam))
                {
                    return p.networkView.owner;
                }
            }
            return np;
        }

    }
}
